package Application.UIEditApplication.view.ui
{
	import Application.UIEditApplication.def.Def;
	import Application.UIEditApplication.def.Manager;
	
	import XComponent.show.*;
	
	import com.utils.keymanager.Key;
	import com.utils.keymanager.KeyCombo;
	
	import def.XG;
	
	import fl.containers.ScrollPane;
	import fl.controls.Button;
	import fl.controls.Label;
	import fl.controls.LabelButton;
	import fl.controls.ScrollBar;
	import fl.core.UIComponent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventPhase;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import manager.XMouseManager;
	
	import org.puremvc.as3.interfaces.INotification;

	public class EditUIMed	extends UIBaseMed
	{
		static public var NAME : String = "EditUIMed";
		
		private var m_editPan : ScrollPane;
		private var m_uiEditPan : UIComponent;
		private var m_lastPan : *;
		
		private var m_components : Array;
		private var m_componentXMLItem : Array;
		private var m_lastBorder : XEmphasizedBorder;
		
		private var m_mouseHandClass : *;
		private var m_curComponent : *;
		
		private var m_componentXML : XML;
		
		private var m_rKey : KeyCombo;
		
		public function EditUIMed()
		{
			super(NAME,[Def.STAGE_ALIGN_LEFT,Def.STAGE_ALIGN_TOP]);
		}
		
		override public function listNotificationInterests():Array
		{
			var orgArry : Array = super.listNotificationInterests();
			
			orgArry.push(Def.MOUSE_HANDBOXCLASSCHANGED);
			orgArry.push(ComponentPropertyMed.COMPONENTPROPERTYCHANGED);

			return orgArry;
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case Def.MOUSE_HANDBOXCLASSCHANGED: 
					
					m_mouseHandClass = notification.getBody();
					
					break;
				case ComponentPropertyMed.COMPONENTPROPERTYCHANGED:
					m_lastBorder.autoSize();
					break;
			}
		}
		
		override public function onRegister():void
		{
			setOffset(125,30);
			
			m_editPan = 	new ScrollPane;
			
			m_components = 	[];
			m_componentXMLItem = [];
			
			setEditPanSize();
			
//			m_uiEditPan = 	new XFrame(m_editPan.width-2,m_editPan.height-2);
			m_uiEditPan = new UIComponent;
			
			m_editPan.scrollDrag = true;
			m_editPan.source = m_uiEditPan;
			
			m_componentXML = <Components></Components>;
			
			m_rKey = Manager.KM.createKeyCombo(Key.R);
		
			m_rKey.addEventListener(KeyCombo.COMBO_PRESSED,OnRemoveComponent);
			
			m_editPan.addEventListener(MouseEvent.MOUSE_DOWN,OnMDUIEditPan);
			m_editPan.addEventListener(MouseEvent.MOUSE_UP,OnMUUIEditPan);
			
			Manager.Mouse.addFun(NAME,ChangeComponentSize,XMouseManager.FUNTYPE_MOUSEMOVE);
			
			var view : Sprite = this.viewComponent as Sprite;
			
			view.addChild(m_editPan);
		}
		
		override public function onRemove():void
		{
			m_rKey.removeEventListener(KeyCombo.COMBO_PRESSED,OnRemoveComponent);
			Manager.KM.removeKeyCombo(m_rKey);
			
			m_editPan.removeEventListener(MouseEvent.MOUSE_DOWN,OnMDUIEditPan);
			m_editPan.removeEventListener(MouseEvent.MOUSE_UP,OnMUUIEditPan);
			
			for each(var component : * in m_components)
			{
				component.removeEventListener(MouseEvent.MOUSE_DOWN,OnMDUIEditPan);
			}
			
			m_editPan = 			null;
			m_uiEditPan = 			null;
			
			m_components = 			null;
			m_componentXMLItem =	null;
			
			m_mouseHandClass = 		null;
			m_componentXML = 		null;
		}
		
		override public function StageSizeChanged():void
		{
			setEditPanSize();
			 
			super.StageSizeChanged();
		}
		
		public function setEditPanSize() : void
		{	
			if(!m_editPan) return;
			
			m_editPan.width = 		XG.stage.stageWidth - 125;
			m_editPan.height = 		XG.stage.stageHeight - 30;
		}
		 
		private function setEmphasizeBorder(pan:*,type:int):void
		{	
			if(!pan) return;
			if(pan == m_curComponent) return;
			
			if(pan != m_uiEditPan)
			{
				m_curComponent = pan;
				
				var border : XEmphasizedBorder = new XEmphasizedBorder;
				border.setEmphasizedBorder(pan,type);
				
				border.addEventListener(XEmphasizedBorder.STATE_CHANGDED,OnSelectRectDot);
				
				m_lastPan = pan;
				
				if(m_lastBorder != border)
				{
					if(m_lastBorder)
					{
						m_lastBorder.removeEventListener(XEmphasizedBorder.STATE_CHANGDED,OnSelectRectDot);
						m_lastBorder.destory();
						m_lastBorder = null;
					}
					m_lastBorder = border;
				}
			}
		}
		
		public function OnMDUIEditPan(evt : MouseEvent) : void
		{
			evt.stopImmediatePropagation();
			
			if(m_lastBorder)
			{
				m_lastBorder.OnMDRectDot(evt);
			}
			
			var pan : * = evt.currentTarget ; 
			
			for(var i:int=0;i<m_components.length;i++)
			{
				if(m_components[i] is DisplayObject)
				{
					if(m_components[i] == pan)
					{
						break;
					}
				}
				else if(m_components[i] is UIBaseMed)
				{
					if(m_components[i].viewSprite == pan)
					{
						pan =	m_components[i];
						break;
					}
				}
			}
			
			if(pan == m_editPan)
			{
				pan = m_uiEditPan;
			}
			else
			{
				openPropertyMed(pan);
			}
		
			setEmphasizeBorder(pan,XEmphasizedBorder.NORMAL);
			
			if(!m_mouseHandClass) return;
			
			var component : * = new m_mouseHandClass;
			
			component.mouseChildren = true;
	
			component.x = pan.mouseX + 1;
			component.y = pan.mouseY + 1;
			
			setEmphasizeBorder(component,XEmphasizedBorder.NORMAL);
			
			if(pan is UIBaseMed)
			{
				pan = pan.viewSprite;
			}
			
			openPropertyMed(component);
			
			addComponent(component,pan);
		}
		
		public function showComponentProperty(component:* = null) : void
		{
			if(XG.hasUI(ComponentPropertyMed.NAME))
			{
				var comPro : ComponentPropertyMed = XG.getUI(ComponentPropertyMed.NAME);
			}
			else
			{
				comPro = XG.getUI(ComponentPropertyMed.NAME);
			}
			
			var className : String;
			if(m_mouseHandClass)
			{
				className = m_mouseHandClass.toString();
				className = className.replace("[class ","");
				className = className.replace("]","");
				comPro.setComponentProperty(className,component);
			}
			else if(component)
			{
				className  = component.toString();
				className = className.replace("[object ","");
				className = className.replace("]","");
				comPro.setComponentProperty(className,component);
			}
			else
			{
				comPro.changeComponentProperty();
			}
		}
		
		private function openPropertyMed(component:*) : void
		{
			XG.openUI(ComponentPropertyMed.NAME,ComponentPropertyMed);
			
			showComponentProperty(component);
		}
		
		public function OnMUUIEditPan(evt : MouseEvent) : void
		{
			if(m_lastBorder)
			{
				m_lastBorder.OnMURectDot(evt);
				
				showComponentProperty(m_curComponent);
			}
		}
		
		/** 
		 *添加组件 
		 * @param component
		 * @param pan
		 * 
		 */		
		private function addComponent(component:*,pan:Sprite):void
		{
			m_curComponent = component;
			
			m_components.push(component);
			
			var itemXML:XML = new XML;
			
			m_curComponent.addEventListener(MouseEvent.MOUSE_DOWN,OnMDUIEditPan);
			
			if(component is XLoader)
			{
				(component as XLoader).load();
			}
			
			var tempComponent : *;
			
			if(m_curComponent is UIBaseMed)
			{
				tempComponent = (m_curComponent as UIBaseMed).viewSprite;
			}
			else
			{
				tempComponent = m_curComponent;
			}
			
			pan.addChild(tempComponent);
			
			m_mouseHandClass  = null;
			
			XG.hideMouseHand();
		}
		
		/**
		 *删除组件 
		 * @param component
		 * @param pan
		 * 
		 */		
		private function removeComponent(component:*) : void
		{
			if(!component) return;
			
			for(var i:int=0;i<m_components.length;i++)
			{
				if(m_components[i] == component)
				{
					m_components.splice(i,1);
					m_componentXMLItem.splice(i,1);
					break;
				}
			}
			
			if(m_lastBorder.hasEventListener(XEmphasizedBorder.STATE_CHANGDED))
			{
				m_lastBorder.removeEventListener(XEmphasizedBorder.STATE_CHANGDED,OnSelectRectDot);
			}
			
			if(component.parent)
			{
				component.removeEventListener(MouseEvent.MOUSE_DOWN,OnMDUIEditPan);
				
				if(component is UIBaseMed)
				{
					component = component.viewSprite;
				}
				
				component.parent.removeChild(component);
				
				m_curComponent = null;
			}
		}
		
		private function OnRemoveComponent(evt : Event) : void
		{
			removeComponent(m_curComponent);
		}
		
		private function OnSelectRectDot(evt : Event) : void
		{
			switch(m_lastBorder.curState)
			{
				case XEmphasizedBorder.STATE_DRAG:
					
					m_curComponent.startDrag();
					m_isDragComponent = true;
					
					break;
				case XEmphasizedBorder.STOPDRAGRECTDOT:
					
					m_curComponent.stopDrag();
					m_isDragComponent = false;
					
					if(m_uiEditPan.width < m_curComponent.x + m_curComponent.width)
					{
						m_uiEditPan.width = m_curComponent.x + m_curComponent.width;
					}
					
					if(m_uiEditPan.height < m_curComponent.y + m_curComponent.height)
					{
						m_uiEditPan.height = m_curComponent.y + m_curComponent.height;
					}
					
					m_editPan.update();
					
					break;
			}
		}
//==================================================================================================	
		private var m_oldMX : Number;
		private var m_oldMY : Number;
		private var m_curMX : Number;
		private var m_curMY : Number;
		private var m_isDragComponent : Boolean;
		
		private function ChangeComponentSize(evt : MouseEvent) : void
		{
			m_oldMX = m_curMX;
			m_oldMY = m_curMY;
			
			m_curMX = m_uiEditPan.mouseX;
			m_curMY = m_uiEditPan.mouseY;
			
			var addWidth : Number = m_curMX - m_oldMX;
			var addHeight : Number = m_curMY - m_oldMY;
		
			//startDarg()方法UIComponent没有更新元件的坐标故用此来更行元件拖动时的坐标
			if(m_isDragComponent)
			{
				if(m_curComponent is UIComponent)
				{
					m_curComponent.x += addWidth;
					m_curComponent.y += addHeight;
				}
			}
			
			if(m_lastBorder)
			{
				var addX:Boolean;
				var addY:Boolean;
				
				switch(m_lastBorder.DragXState)
				{
					case XEmphasizedBorder.DRAGSTATE_LEFT:
						
						addX = true;
						
						addWidth = -addWidth;
						
						m_curComponent.x = m_curComponent.parent.mouseX;
						
						break;
					case XEmphasizedBorder.DRAGSTATE_RIGHT:
						addX = true;
						break;
					default : addWidth = 0;break;
				}	
				
				switch(m_lastBorder.DragYState)
				{ 
					case XEmphasizedBorder.DRAGSTATE_DOWN:
						addY = true;
						break;
					case XEmphasizedBorder.DRAGSTATE_UP:
						
						addY = true;
						
						addHeight = -addHeight;
						
						m_curComponent.y = m_curComponent.parent.mouseY;
						
						break;
					default : addHeight = 0;break;
				}
				
				if(addX)
				{
					m_curComponent.width += addWidth;
				}
				
				if(addY)
				{
					m_curComponent.height += addHeight;
				}
				
				if(addX || addY)
				{
					m_lastBorder.autoSize();
					
					if(m_curComponent is XPopWnd)
					{
						m_curComponent.update();
					}
				}
			}
		}
	}
}